package com.tianzeculture.stagebao.activity.business;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.adapter.MyPagerAdapter;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.util.DensityUtil;
import com.tianzeculture.stagebao.util.StatusBarUtil;
import com.tianzeculture.stagebao.util.Util;

import java.util.ArrayList;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/6/26 0026.
 * 商人主页
 */

public class BusinessActivity extends BaseActivity implements BusinessDataFragment.FragmentInteraction {

    @BindView(R.id.iv_header_bg_ext)
    ImageView ivHeaderBgExt;
    @BindView(R.id.iv_header_bg)
    ImageView mIvHeaderBg;
    @BindView(R.id.iv_pi_header)
    CircleImageView ivPiHeader;
    @BindView(R.id.rl_header)
    RelativeLayout rlHeader;

    @BindView(R.id.rl_avatar)
    RelativeLayout mRlavatar;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
//    @BindView(R.id.fl_toolbar)
//    FrameLayout mFlToolbar;

    @BindView(R.id.title)
    RelativeLayout titleLayout;
    @BindView(R.id.titleTransparent)
    RelativeLayout transparentTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.appBarlayout)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    //    @BindView(R.id.tv_pi_name)
//    TextView tvPiName;
    @BindView(R.id.tvArtistName)
    TextView tvArtistName;
    @BindView(R.id.tvCity)
    TextView tvCity;
    @BindView(R.id.tvCompany)
    TextView tvCompany;
    @BindView(R.id.back)
    ImageButton back;
    private MyPagerAdapter mPagerAdapter;

    @Override
    public void initUI() {
        super.initUI();
        initToolbar();
        initRefresh();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ArrayList<Fragment> mListFragment = new ArrayList<>();
        String[] titles = {""};
        mListFragment.add(new BusinessDataFragment());

        mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mListFragment);
        mPagerAdapter.setTitles(titles);
        mViewPager.setAdapter(mPagerAdapter);
        Util.setIndicator(mTabLayout, 25, 25);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    public void updateUI(User user) {

    }

    @Override
    protected void onResume() {
        super.onResume();
//        showProgressDialog();
//        businessRestUsage.toSellerdetails(toSellerdetails, getIntent().getStringExtra("userId"));


    }

    private void initToolbar() {
        tvTitle.setText("商家信息");
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        int statusHeight = StatusBarUtil.getStatusBarHeight(this);

        toolbar.measure(0, 0);
        int measuredHeight = toolbar.getMeasuredHeight();

        ViewGroup.LayoutParams layoutParams = toolbar.getLayoutParams();
        layoutParams.height = measuredHeight + statusHeight;
        toolbar.setLayoutParams(layoutParams);


        ViewGroup.LayoutParams params = titleLayout.getLayoutParams();
        params.height = measuredHeight + statusHeight;
        titleLayout.setLayoutParams(params);

        ViewGroup.LayoutParams params2 = transparentTitle.getLayoutParams();
        params2.height = measuredHeight + statusHeight;
        transparentTitle.setLayoutParams(params2);

    }

    private void initRefresh() {
        mIvHeaderBg.setScaleX(1.3f);
        mIvHeaderBg.setScaleY(1.3f);

        final int[] location = new int[2];
        ivPiHeader.getLocationInWindow(location);
        final int endOffset = DensityUtil.dip2px(this, 200);

        final int[] pointV = new int[2];
        tvArtistName.getLocationInWindow(pointV);
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int[] ponitF = new int[2];
                int disten = -verticalOffset;
                int disOffset = endOffset;
                if (disten >= disOffset) {
                    mRlavatar.setAlpha(0);
                    mIvHeaderBg.setScaleX(1.0f);
                    mIvHeaderBg.setScaleY(1.0f);
                    titleLayout.setVisibility(View.VISIBLE);
                    transparentTitle.setVisibility(View.GONE);
                } else if (disten == 0) {
                    mRlavatar.setAlpha(1);
                    mIvHeaderBg.setScaleX(1.3f);
                    mIvHeaderBg.setScaleY(1.3f);
                } else {
                    float precents = (float) (disOffset - disten) / disOffset;
                    mRlavatar.setAlpha(precents);

                    mIvHeaderBg.setScaleX(1.0f + 0.3f * (precents));
                    mIvHeaderBg.setScaleY(1.0f + 0.3f * (precents));

                    titleLayout.setVisibility(View.GONE);
                    transparentTitle.setVisibility(View.VISIBLE);
                }

                ivPiHeader.getLocationInWindow(ponitF);

                int y = ponitF[1];
                if (y > 0) {
                    if (y < endOffset) {
//                        titleLayout.setVisibility(View.GONE);
//                        transparentTitle.setVisibility(View.VISIBLE);
//                        toolbar.setBackgroundResource(R.color.colorTransparent);
                        float precent = (float) (endOffset - y) / endOffset;
                        toolbar.setAlpha(precent);
                    } else if (y >= endOffset) {
                        toolbar.setAlpha(0);
//                        toolbar.setBackgroundResource(R.color.colorGray_F0);
//                        titleLayout.setVisibility(View.GONE);
//                        transparentTitle.setVisibility(View.VISIBLE);
                    }
                } else if (y < 0) {
                    toolbar.setAlpha(1);
//                    toolbar.setBackgroundResource(R.color.colorGray_F0);
//                    titleLayout.setVisibility(View.VISIBLE);
//                    transparentTitle.setVisibility(View.GONE);
                } else if (y == 0) {
//                    titleLayout.setVisibility(View.VISIBLE);
//                    transparentTitle.setVisibility(View.GONE);
//                    toolbar.setBackgroundResource(R.color.colorTransparent);
                    toolbar.setAlpha(1);
                }


            }
        });


    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);

    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_business;
    }

    @Override
    public void process(User user) {
        tvArtistName.setText(user.getNickname());
        tvCity.setText(user.getAddress());
        Glide.with(this).load(PICTURE_URL + user.getImgurl()).error(R.mipmap.icon_head).into(ivPiHeader);
        tvCompany.setText(R.string.company);
    }
}
